package com.sjh.tomcat.servlet;

import com.sjh.tomcat.http.SjhRequest;
import com.sjh.tomcat.http.SjhResponse;
import com.sjh.tomcat.utils.WebUtils;

import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.OutputStream;

public class SjhCalServlet extends SjhHttpServlet{

    @Override
    public void doGet(SjhRequest request, SjhResponse response) {
        int num1 = WebUtils.parseInt(request.getParameter("num1"), 0);
        int num2 = WebUtils.parseInt(request.getParameter("num2"), 0);
        int res= num1 + num2;
        OutputStream outputStream = response.getOutputStream();
        String mes = SjhResponse.respHeader + "<h1>"+num1+" + "+num2+" = "+res+"</h1>";
        try {
            outputStream.write(mes.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(SjhRequest request, SjhResponse response) {
        this.doGet(request, response);
    }

    @Override
    public void init(ServletConfig var1) throws Exception {

    }

    @Override
    public void destroy() {

    }
}
